package cn.zyq.ykb.controller.h5.config;

import cn.hutool.core.bean.BeanUtil;
import cn.zyq.ykb.beans.config.ConfigInfo;
import cn.zyq.ykb.beans.config.ConfigNotice;
import cn.zyq.ykb.mapper.config.ConfigInfoMapper;
import cn.zyq.ykb.mapper.config.ConfigNoticeMapper;
import cn.zyq.ykb.service.config.ConfigNoticeService;
import cn.zyq.ykb.system.annotation.Log;
import cn.zyq.ykb.system.common.BaseController;
import cn.zyq.ykb.system.enums.core.SortEnum;
import cn.zyq.ykb.system.ry.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 配置-公告提醒Controller
 *
 * @author SystemAuth
 * @date 2023-12-04
 */

@RestController(value = "H5ConfigNoticeController")
@RequestMapping("/api/configNotice")
@Api(tags = "配置-公告提醒接口")
@Slf4j
public class ConfigNoticeController extends BaseController {

    @Autowired
    private ConfigNoticeService configNoticeService;
    @Autowired
    private ConfigNoticeMapper configNoticeMapper;
    @Autowired
    private ConfigInfoMapper configInfoMapper;


    @ApiOperation(value = "查询公告列表", response = ConfigNotice.class)
    @GetMapping("/list")
    public Object list() {
        ConfigInfo configInfo = new ConfigInfo();
        configInfo.setDeleted(0);
        configInfo.setState(SortEnum.YSS.getValue());
        return this.configInfoMapper.select(configInfo);
    }


    /**
     * 查询配置-公告提醒列表
     */
    @ApiOperation(value = "查询是否展示", response = ConfigNotice.class)
    @GetMapping("/selectByID")
    public Object selectByID() {
        Integer userID = this.getLoginUser().getUserID();
        ConfigNotice notice = new ConfigNotice();
        notice.setUserID(userID);
        notice.setDeleted(0);
        ConfigNotice configNotice = this.configNoticeMapper.selectOne(notice);
        ConfigInfo configInfo = new ConfigInfo();
        configInfo.setDeleted(0);
        configInfo.setState(SortEnum.YSS.getValue());
        ConfigInfo info = this.configInfoMapper.selectOne(configInfo);
        if (BeanUtil.isNotEmpty(info)){
            String text = Jsoup.parse(info.getNote()).text();
            log.info("【转换后：】{}",text);
            info.setConvert(text);
        }
        if (BeanUtil.isEmpty(configNotice)){
            notice.setRemind(0);
            notice.setConfigInfo(info);

            return notice;
        }else{
            configNotice.setConfigInfo(info);
        }
        return configNotice;
    }
    /**
     * 新增配置-公告提醒
     */
    @ApiOperation(value = "新增")
    @Log(title = "配置-公告提醒", businessType = BusinessType.INSERT)
    @PostMapping
    public Object add(@RequestBody ConfigNotice configNotice) {
        return this.configNoticeService.insertConfigNotice(configNotice,this.getLoginUser());
    }

}
